<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Test;

use App\Controller\AbstractController;
use App\Middleware\JWTAuthMiddleware;
use App\Model\User;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Qbhy\HyperfAuth\AuthManager;

#[Controller('/test')]
class AuthTestController extends AbstractController
{
    #[Inject]
    public AuthManager $auth;

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return $response->raw('Hello Hyperf!');
    }

    #[RequestMapping(path: 'session/login', methods: 'post')]
    public function sessionLogin()
    {
        /** @var User $user */
        // $user = User::query()->firstOrCreate(['name' => 'test', 'avatar' => 'avatar']);
        $email = $this->request->input('email');
        $password = $this->request->input('password');
        $user = User::query()->where('email', $email)->where('password', $password)->firstOrFail();
        return [
            'status' => $this->auth->guard('session')->login($user),
        ];
    }

    #[RequestMapping(path: 'jwt/login', methods: 'post')]
    public function jwtLogin()
    {
        // $user = User::query()->firstOrCreate(['name' => 'test', 'avatar' => 'avatar']);
        $email = $this->request->input('email');
        $password = $this->request->input('password');
        $user = User::query()->where('email', $email)->where('password', $password)->firstOrFail();
        return [
            'token' => $this->auth->guard()->login($user),
        ];
    }

    #[RequestMapping(path: 'sso/login', methods: 'post')]
    public function ssoLogin()
    {
        /** @var User $user */
        // $user = User::query()->firstOrCreate(['name' => 'test', 'avatar' => 'avatar']);
        $email = $this->request->input('email');
        $password = $this->request->input('password');
        $user = User::query()->where('email', $email)->where('password', $password)->firstOrFail();
        return [
            'token' => $this->auth->guard('sso')->login($user, [], 'pc'), // sso 方法支持第二个参数，传定义好的客户端
        ];
    }

    #[Middlewares([JWTAuthMiddleware::class])]
    #[RequestMapping(path: 'user', methods: 'get')]
    public function user()
    {
        $type = $this->request->input('type');
        var_dump($type);
        var_dump(auth($type)->guest());
        return $this->auth->guard($type)->user();
    }

    #[RequestMapping(path: 'logout', methods: 'get')]
    public function logout()
    {
        $type = $this->request->input('type');
        return $this->auth->guard($type)->logout();
    }
}
